<template>
  <div class="app-container" id="follow">
    <el-form ref="form" :model="form" :rules="rules" label-width="179px" class="demo-form-inline">
      <el-card class="box-card a1">
        <p><i class="el-icon-edit" style="width: 20px"></i>老年衰弱（肌少症）实验室筛查血斑退回登记<span style="margin-left: 20px"></span></p>
        <el-form-item label="姓名">
          <el-input v-model="form.username"></el-input>
        </el-form-item>
          <el-form-item label="年龄">
          <el-input v-model="form.age"></el-input>
        </el-form-item>
        <el-form-item label="现住地址">
          <el-input v-model="form.address"></el-input>
        </el-form-item>
        <el-form-item label="联系电话">
          <el-input v-model="form.tel"></el-input>
        </el-form-item>
        <el-form-item label="血斑接收日期">
          <el-date-picker clearable v-model="form.receiveTime" type="datetime" format="yyyy-MM-dd" value-format="yyyy-MM-dd"  placeholder="请选择血斑接收日期">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="采血日期">
          <el-date-picker clearable v-model="form.bloodTime" type="datetime" format="yyyy-MM-dd" value-format="yyyy-MM-dd"  placeholder="请选择采血日期">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="采血医院">
          <el-input v-model="form.hospital"></el-input>
        </el-form-item>
        <el-form-item label="退回原因">
          <el-input v-model="form.returnReason"></el-input>
        </el-form-item>
        <el-form-item label="退回日期">
          <el-date-picker clearable v-model="form.returnTime" type="datetime" format="yyyy-MM-dd" value-format="yyyy-MM-dd"  placeholder="请选择退回日期">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="筛查中心">
          <el-input v-model="form.screeningUnit"></el-input>
        </el-form-item>
        <el-form-item label="采血机构">
          <el-input v-model="form.bloodUnit"></el-input>
        </el-form-item>
        <el-form-item label="录入时间">
          <el-input v-model="form.createTime" disabled></el-input>
        </el-form-item>
      </el-card>

      <el-form-item style="margin-top: 15px">
        <el-button type="primary" @click="onSubmit">保存</el-button>
        <el-button @click="onCancel">取消</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
import { getFollow, addFollow, updateFollow, listFollow } from '@/api/his/follow'
import { tansParams } from '@/utils/ruoyi'
import axios from 'axios'
import { getToken } from '@/utils/auth'
import { getPatient } from '@/api/his/patient'

export default {
  name: 'PatientTh',
  dicts: [],
  data() {
    return {
      btnLoading: false,
      form: {
        id:null,
        followId:null,
        username:null,
        age:null,
        address:null,
        tel:null,
        patientId:null,
        screeningItem:'',
        hospital:'',
        returnReason:'',
        screeningUnit:'',
        bloodUnit:'',
        receiveTime:'',
        bloodTime:'',
        returnTime:'',
        status:'', //3:血斑退回
      },
      follow:{},
      // 表单校验
      rules: {}
    }
  },
  created() {
    this.init();
  },
  mounted() {
  },
  methods: {
    init(){
      const id = this.form.patientId = this.$route.params && this.$route.params.id;
          //getFollow(id).then(response => {
      listFollow({patientId:id,status:this.form.status}).then(response => {
            //console.log('response: '+JSON.stringify(response))
            if(response.total>0) {
              var status = this.form.status = response.rows[0].status == null ? '' : response.rows[0].status
              if (status.indexOf('3') == -1) {
                this.form.status = status + '@' + '3' //回填
              }
            }else{
              this.form.status = '3'
            }
            if(response.rows!=null && response.rows[0]!=null) {
              this.form.followId = this.form.id = response.rows[0].id;
              this.form.screeningItem = response.rows[0].screeningItem;
              this.form.username = response.rows[0].username
              this.form.age = response.rows[0].age
              this.form.address= response.rows[0].usual
              this.form.tel= response.rows[0].telephone
              this.form.hospital = response.rows[0].hospital
              this.form.receiveTime = response.rows[0].receiveTime
              this.form.bloodTime = response.rows[0].bloodTime
              this.form.returnReason = response.rows[0].returnReason
              this.form.returnTime = response.rows[0].returnTime
              this.form.bloodUnit = response.rows[0].bloodUnit; //采血机构
              this.form.screeningUnit = response.rows[0].screeningUnit; //筛查中心
              this.form.createTime = response.rows[0].createTime;
            }else{
              getPatient(id).then(response => {
                this.form.username = response.data.patientName
                this.form.age = response.data.age
                this.form.address= response.data.address
                this.form.tel= response.data.tel
                this.form.hospital = response.data.unit
                this.form.ctime = response.data.ctime
                this.form.createTime = response.data.createTime;
                this.form.updateTime = Date.now();
              })
            }
          });

    },
    onSubmit() {
      //提交表单
      this.$refs["form"].validate(valid => {
        this.form.id = this.form.followId;
        this.form.usual = this.form.address;
        this.form.telephone = this.form.tel;
        if (this.form.followId != null) {
          this.form.updateTime = Date.now();
          updateFollow(this.form).then(response => {
            this.$modal.msgSuccess("添加成功");
          });
        } else {
          //console.log('this.form==>',JSON.stringify(this.form))
          addFollow(this.form).then(response => {
            this.$modal.msgSuccess("新增成功");
          });
        }
        //关闭tab
        this.$store.dispatch("tagsView/delView", this.$route);
        this.$router.go(-1);
      })
    },
    delView({ dispatch, state }, view) {
      return new Promise(resolve => {
        dispatch('delVisitedView', view)
        dispatch('delCachedView', view)
        resolve({
          visitedViews: [...state.visitedViews],
          cachedViews: [...state.cachedViews]
        })
      })
    },    // 表单重置
    reset() {
      this.form = {
      };
      this.resetForm("form");
    },

    onCancel(){
      //关闭tab
      this.$store.dispatch("tagsView/delView", this.$route);
      this.$router.go(-1);
    },

  }
}
</script>


<style lang="scss" scoped>
#follow .el-form-item {
  margin-bottom: 5px !important;
}

.inner_div {
  display: flex;
  ::v-deep {
    .el-input {
      width: 50%;
      display: inline-block;
    }
  }
}
.inner_div2 {
  display: flex;
  ::v-deep {
    .el-input {
      width: 40%;
      display: inline-block;
    }
  }
}
</style>
